/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package conversormonetario;

import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Admin1
 */

public class ListaMoedas 
{
    
    static ArrayList<Moeda> Moedas;
    String Ultima_Actualizacao;
    static ArrayList<String> datas;
    
    ListaMoedas(int n)
    {
        FileReader fr = null;
        try {
            fr = new FileReader(new File("arquivo.txt"));
            BufferedReader br = new BufferedReader(fr);
            
            String linha = br.readLine();
            int p = Integer.parseInt(linha);
            linha = br.readLine();
            this.Ultima_Actualizacao = linha;
            this.Moedas = new ArrayList<>();
            this.datas = new ArrayList<>();
            for(int i=0;i<p;i++)
            {
                String Ini,Tit;
                double T1, T2,h1;
                int th;
                Moeda m;
                ArrayList<Double> h= new ArrayList<>();
                linha = br.readLine();
                Ini = linha;
                linha = br.readLine();
                Tit = linha;
                linha = br.readLine();
                T1 = Double.parseDouble(linha);
                linha = br.readLine();
                T2 = Double.parseDouble(linha);
                linha = br.readLine();
                th = Integer.parseInt(linha);
                for(int j=0;j<th;j++)
                {
                    linha = br.readLine();
                    h1 = Double.parseDouble(linha);
                    h.add(h1);
                }
                m = new Moeda(Ini, Tit, T1, T2,h);
                this.Moedas.add(m);
            }
            int td;
            linha = br.readLine();
            td = Integer.parseInt(linha);
            for (int j = 0; j < td; j++) {
                linha = br.readLine();
                String g = linha; 
                this.datas.add(g);
            }
            
        } catch (IOException ex) 
        {
            Logger.getLogger(ListaMoedas.class.getName()).log(Level.SEVERE, null, ex);
            
        } finally {
            try {
                fr.close();
            } catch (IOException ex) {
                Logger.getLogger(ListaMoedas.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
    }
    ListaMoedas()
    {
        Moeda m;
        this.Moedas = new ArrayList<>();
        for(int i=0;i<TodasMoedas.strings.length;i++)
        {
            m = new Moeda(TodasMoedas.strings[i], TodasMoedas.strings2[i]);
            this.Moedas.add(m);
        }
        this.Ultima_Actualizacao = "nao existe";
        this.datas = new ArrayList<>();
    }
    
    boolean GravaDados() 
    {
        PrintWriter pw = null;
        try {
            pw = new PrintWriter( new File( "arquivo.txt" ) );
            
            pw.println(this.Moedas.size());
            pw.println(this.Ultima_Actualizacao);
            for(int i=0;i<this.Moedas.size();i++)
            {
                pw.println(this.Moedas.get(i).getIniciais());
                pw.println(this.Moedas.get(i).getTitulo());
                pw.println(this.Moedas.get(i).getTaxa_Euro_para_x());
                pw.println(this.Moedas.get(i).getTaxa_x_para_Euro());
                pw.println(this.Moedas.get(i).historico.size());
                for(int j=0;j<this.Moedas.get(i).historico.size();j++)
                    pw.println(this.Moedas.get(i).historico.get(j));   
            }
            pw.println(this.datas.size());
            for(int i=0;i<this.datas.size();i++)
                pw.println(this.datas.get(i));
        } catch (FileNotFoundException ex) 
        {
            Logger.getLogger(ListaMoedas.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        } 
        finally 
        {
            pw.close();
        }
        return true;
    }
    void ActualizarTaxas() 
    {
        // Buscar taxas
        for (int i = 0; i < Moedas.size(); i++) 
        {
            this.Moedas.get(i).historico.add(this.Moedas.get(i).getTaxa_x_para_Euro());
                String URL = "http://finance.yahoo.com/q/bc?s=EUR" + this.Moedas.get(i).getIniciais() + "=X&t=5d&l=on&z=m&q=l&c=";
            
            try {

                String results = "";
                URL thePage = new URL(URL);
                BufferedReader in = new BufferedReader(new InputStreamReader(thePage.openStream()));
                String inputLine;
                while ((inputLine = in.readLine()) != null) 
                {
                    results = results + inputLine;
                }
                in.close();
                Pattern finalPattern = Pattern.compile("x\">([0-9.,]+)</span>");
                Matcher matchFind = finalPattern.matcher(results);
                matchFind.find();
                String esp = matchFind.group(1).replace(',', ' ');
                esp = esp.replace(" ", "");
                System.out.print(this.Moedas.get(i).getIniciais() + " - " + esp + "\n");
                double curRate = Double.valueOf(esp).doubleValue();
                this.Moedas.get(i).setTaxa_Euro_para_x(curRate);

            } catch (Exception e ) 
            {
                System.out.print(e);
                return;
            }

            URL = "http://finance.yahoo.com/q/bc?s=" + this.Moedas.get(i).getIniciais() + "EUR=X&t=5d&l=on&z=m&q=l&c=";

            try {
                String results = "";
                URL thePage = new URL(URL);
                BufferedReader in = new BufferedReader(new InputStreamReader(thePage.openStream()));
                String inputLine;
                while ((inputLine = in.readLine()) != null) 
                {
                    results = results + inputLine;
                }
                in.close();
                Pattern finalPattern = Pattern.compile("x\">([0-9.]+)</span>");
                Matcher matchFind = finalPattern.matcher(results);
                matchFind.find();
                String esp = matchFind.group(1).replace(',', ' ');
                esp = esp.replace(" ", "");
                System.out.print(this.Moedas.get(i).getIniciais() + " - " + esp + "\n");
                double curRate = Double.valueOf(esp).doubleValue();
                this.Moedas.get(i).setTaxa_x_para_Euro(curRate);



            } catch (Exception e) 
            {
                System.out.print(e);
                return;
            }
        }
        this.datas.add(this.Ultima_Actualizacao);
        Date date = new Date();
        this.Ultima_Actualizacao = date.toString();
    }
    double getConvercao(String m1, String m2, double n)
    {
        double aux = 0;
        for(int i=0;i<this.Moedas.size();i++)
        {
            if(this.Moedas.get(i).getIniciais().equals(m1))
            {
                aux = calculo(n,this.Moedas.get(i).getTaxa_x_para_Euro());
                break;
            }
            
        }
        for(int i=0;i<this.Moedas.size();i++)
        {
            if(this.Moedas.get(i).getIniciais().equals(m2))
            {
                aux = calculo(aux,this.Moedas.get(i).getTaxa_Euro_para_x());
                break;
            }
        }
        return aux;
    }
    double calculo (double n,double m)
    {
        return n*m;
    }
    int getNumMoedas()
    {
        return this.Moedas.size();
    }
    Moeda getMoeda(int i)
    {
        return this.Moedas.get(i);
    }          
    public String toString() 
    {
        String str = "ListaMoedas{\n";
        for(int i=0;i<Moedas.size();i++)
        {
            str = str +"\t"+i+"--"+ Moedas.get(i).toString();
        }
        str = str + "Ultima_Actualizacao=" + Ultima_Actualizacao + '}'+ "\n";
        return  str;
    }
    int getIndexMoedas (String s)
    {
        for(int i=0;i<this.Moedas.size();i++)
            if(this.Moedas.get(i).getIniciais().equals(s))
                return i;
        return -1;
    }
}
